﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace HiViLicenseManage
{
	public partial class Report : System.Web.UI.Page
	{
		private System.Globalization.CultureInfo culInfo = new System.Globalization.CultureInfo("vi-VN");
		DataTable dtMain;
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		private void LoadData()
		{
			SqlConnection appConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["appconn"].ConnectionString);
			appConn.Open();

			string dfrom = DateTime.Parse(txtDFrom.Text, culInfo).ToString("yyyyMMdd");
			string dto = (DateTime.Parse(txtDTo.Text, culInfo).AddDays(1)).ToString("yyyyMMdd");
			if (txtAgent.Text == "")
			{
				SqlCommand sqlComm;
				sqlComm = new SqlCommand("SELECT FK_AgentID, AgentName, FK_SoftwareID, SoftwareName, COUNT(LicenseKey) AS NumberKey FROM vwLicense WHERE DateCreated >= '" + dfrom + "' AND DateCreated < '" + dto + "' GROUP BY FK_AgentID, AgentName, FK_SoftwareID, SoftwareName ORDER BY FK_AgentID, FK_SoftwareID ASC", appConn);
				sqlComm.CommandType = CommandType.Text;
				SqlDataAdapter sqlDA = new SqlDataAdapter();
				sqlDA.SelectCommand = sqlComm;
				dtMain = new DataTable();
				sqlDA.Fill(dtMain);

				gdvReport.DataSource = dtMain;
				gdvReport.DataBind();
			}
			else
			{
				SqlCommand sqlComm;
				sqlComm = new SqlCommand("SELECT FK_AgentID, AgentName, FK_SoftwareID, SoftwareName, COUNT(LicenseKey) AS NumberKey FROM vwLicense WHERE UPPER(FK_AgentID) LIKE N'%" + txtAgent.Text.ToUpper() + "%' AND DateCreated >= '" + dfrom + "' AND DateCreated < '" + dto + "' GROUP BY FK_AgentID, AgentName, FK_SoftwareID, SoftwareName ORDER BY FK_AgentID, FK_SoftwareID ASC", appConn);
				sqlComm.CommandType = CommandType.Text;
				SqlDataAdapter sqlDA = new SqlDataAdapter();
				sqlDA.SelectCommand = sqlComm;
				dtMain = new DataTable();
				sqlDA.Fill(dtMain);

				gdvReport.DataSource = dtMain;
				gdvReport.DataBind();
			}
		}

		protected void btnOk_Click(object sender, EventArgs e)
		{
			LoadData();
		}

		protected void gdvReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
		{
			gdvReport.PageIndex = e.NewPageIndex;
			gdvReport.DataBind();
		}
	}
}
